/*
 * Copyright (c) 2010, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * kiddie-cpp
 * Created on: 17.04.2010
 *     Author: artur
 *
 */

#ifndef HASH_MAP_H_
#define HASH_MAP_H_


const int TABLE_SIZE = 28;

#ifndef NULL
#define NULL 0
#endif

/**
 * @namespace util
 * @brief Helper functions, classes and data structures.
 *
 */
namespace util
{

	/**
	 * @class hash_entry
	 * @brief Hash entry of hash_map template.
	 *
	 */
	template<typename T>
	class hash_entry
	{
		private:
			int key;
			T value;

		public:
			hash_entry(int key, T value)
			{
				key = key;
				value = value;
			}

			int getKey()
			{
				return key;
			}

			T getValue()
			{
				return value;
			}
	};

	/**
	 * @class hash_map
	 * @brief A hashed map class template.
	 *
	 * A hash map template. Widely used in internal structures
	 * of kernel, IRs, resource mappings and others.
	 */
	template<typename T>
	class hash_map
	{
		private:
			hash_entry<T> **table;

		public:

			/**
			 * @fn hash_map()
			 * @brief hash map constructor.
			 */
			hash_map()
			{
#if 0
				table = new hash_entry<T>*[TABLE_SIZE];
				for (int i = 0; i < TABLE_SIZE; i++)
					table[i] = NULL;
#else
#warning __LINE__ " Use hash_map() just after implementing \
	good memory allocators and deallocators."
#endif
			}

			T get(int key)
			{
				int hash = (key % TABLE_SIZE);
				while (table[hash] != NULL && table[hash]->getKey() != key)
					hash = (hash + 1) % TABLE_SIZE;
				if (table[hash] == NULL)
					return -1;
				else
					return table[hash]->getValue();
			}

			void put(int key, T value)
			{
				int hash = (key % TABLE_SIZE);
				while (table[hash] != NULL && table[hash]->getKey() != key)
					hash = (hash + 1) % TABLE_SIZE;
				if (table[hash] != NULL)
					delete table[hash];
				table[hash] = new hash_entry<T>(key, value);
			}

			~hash_map()
			{
#if 0
				for (int i = 0; i < TABLE_SIZE; i++)
					if (table[i] != NULL)
						delete table[i];
				delete[] table;
#else
#warning __LINE__ " Use ~hash_map() just after implementing \
	good memory allocators and deallocators."
#endif
			}
	};

}

#endif /* HASH_MAP_H_ */
